<?php
namespace Wap\Controller;
use Wap\Controller\CommonController;
class ShejiController extends CommonController {

    /**
      +----------------------------------------------------------
     * 首页
     *
      +----------------------------------------------------------
     */
    public function index() {
        
        $zlist=M('zlist');
        $zlist1=$zlist->select();    
      $this->data=$zlist1;
               $idd=I('get.id'); 
                  
                
      $arr['status']=1;
      $arr['zt']=2;
      
       if (is_numeric($idd)) {
        $zlist2=$zlist->where('id='.$idd)->find();
          $arr['fengge']=$zlist2['type_name'];
          

    $count=M('user')->where($arr)->count();
    $Page =new \Think\Page($count,10);
    $Page -> setConfig('first','首页');
    $Page -> setConfig('last','末页');
    $Page -> setConfig('prev','上一页');
    $Page -> setConfig('next','下一页');
    // if($count < $max_count){
    //   $one = "1";

    // }
    $Page -> setConfig('theme',' <div style="margin-top:2px">  %FIRST%  %UP_PAGE%    %LINK_PAGE%  %DOWN_PAGE%   %END% </div> ');

         $sheji=M('user')->where($arr)->order("id desc")->limit($Page->firstRow, $Page->listRows)->select();

          $show = $Page->show();

              foreach ($sheji as &$value) {
                  $ggg=M('shangpin')->where('userid='.$value['id'])->count();
              
                  $value['count']=$ggg;
              }



           }else{


    $count=M('user')->where($arr)->count();
    $Page =new \Think\Page($count,10);
    $Page -> setConfig('first','首页');
    $Page -> setConfig('last','末页');
    $Page -> setConfig('prev','上一页');
    $Page -> setConfig('next','下一页');
    // if($count < $max_count){
    //   $one = "1";

    // }
    $Page -> setConfig('theme',' <div style="margin-top:2px">  %FIRST%  %UP_PAGE%    %LINK_PAGE%  %DOWN_PAGE%   %END% </div> ');

         $sheji=M('user')->where($arr)->order("id desc")->limit($Page->firstRow, $Page->listRows)->select();

          $show = $Page->show();

         foreach ($sheji as &$value) {
                  $ggg=M('shangpin')->where('userid='.$value['id'])->count();
                 
                  $value['count']=$ggg;
              }



     }




        $this->assign('page',$show);
       $this->assign('sheji',$sheji);

		   $this->display(); // 输出模板

  
    }


    public function detail(){
        $id=I('get.id');
      
        $de=M('user')->where('id='.$id)->find();
      $count=M('shangpin')->where('userid='.$de['id'])->count();
         $sheji=M('shangpin')->where('userid='.$de['id'])->select();
     

 $sheji_status = 0;
        $result = M('shoucang')->where(array("userid"=>$_SESSION["userid"]))->find();
        if($result){
          $sheji_shoucang = explode(',',$result['sheji']);
          $is_has = false;
          foreach ($sheji_shoucang as $key => $value) {
            if($value==$id){
              $is_has = true;
              break;
            }
          }
        }
        if($is_has){
          $sheji_status = 1;
        }

    
        $this->assign("sheji1",$sheji_status);




      $this->assign('sheji',$sheji);
        $this->assign('count',$count);
      $this->assign('de',$de);

      $this->display();
    }




public function fangan(){
         $id=I('get.id');
           
   $fangan=M('shangpin')->where('id='.$id)->find();
      
 $fangan['intro']=htmlspecialchars_decode($fangan['intro']);
$this->assign('fangan',$fangan);

      $this->display();
    }



  public function search(){

          $list=M('zlist')->select();
          
          $huxing=M('huxing')->order('id asc')->select();
          
          $fengge = I("get.fengge");
       $huxing1 = I("get.huxing");
       $where = "";

       if(!empty($fengge)){
          $data = M('zlist')->where("id = $fengge")->find();
          if(empty($where)){
            $where .= "a.fengge = '".$data['type_name']."'";
          }else{
            $where .= " and a.fengge = '".$data['type_name']."'";
          }
          
           
       }

       if(!empty($huxing1)){
          $data1 = M('huxing')->where("id=$huxing1")->find();
          if(empty($where)){
            $where .= "a.huxing = '".$data1['huxing']."'";
          }else{
            $where .= " and a.huxing = '".$data1['huxing']."'";
          }
       }
       
       $title=I('post.title');
         $map['a.name'] =array('like',"%$title%");
          $map['a.fengge'] =array('like',"%$title%");
          $map['a.huxing'] =array('like',"%$title%");
          $map['_logic'] = 'or';
    $shangpin=M('shangpin')->alias("a")->field("a.id,a.name,a.fengge,a.huxing,a.area,a.price,a.intro,a.img,a.userid,b.user as username")->join("zyb_user b on b.id = a.userid")->where($map)->order("a.id desc")->select();
         
            $this->assign("fengge",$fengge);
      $this->assign('list',$list);
      $this->assign('huxing',$huxing);
      $this->assign('huxing1',$huxing1);
            $this->assign('shangpin',$shangpin);
            $this->display();
        
    }  
    


}
 
?>